/**
 * 
 */
package dms.presentation.action.student;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dms.dto.PurchaseItemDTO;
import dms.model.Dormitory;
import dms.model.form.PurchaseItem;
import dms.model.role.wrapper.StudentWrapper;
import dms.model.util.PageIterator;
import dms.service.PurchaseItemQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.DateFormatUtil;
import dms.util.PageUtil;

/**
 * @author 李健
 *
 */
public class PurchaseItemAction extends StudentBaseAction {
	
	private final static Log log = LogFactory.getLog(PurchaseItemAction.class);
	public ActionForward newPurchaseItem(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentWrapper studentWrapper = this.getStudentWrapper();
		request.setAttribute("student", studentWrapper.getStudent());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addPurchaseItem(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentWrapper studentWrapper = this.getStudentWrapper();
		PurchaseItemDTO purchaseItemDTO = (PurchaseItemDTO) form;
		JSONObject jsonObject = null;
		try {
			studentWrapper.addPurchaseItem(purchaseItemDTO);
			jsonObject = this.makeJSONObject(true, "添加成功...");
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "添加失败...");
		}
		
		response.setContentType("application;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		return null;
	}
	
	public ActionForward updatePurchaseItem(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentWrapper studentWrapper = this.getStudentWrapper();
		PurchaseItemDTO purchaseItemDTO = (PurchaseItemDTO) form;
		JSONObject jsonObject = null;
		
		try {
			studentWrapper.updatePurchaseItem(purchaseItemDTO);
			jsonObject = this.makeJSONObject(true, "更新成功...");
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "更新失败" + e.getMessage() + "...");
		}
		
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		
		return null;
	}
	
	public ActionForward deletePurchaseItem(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("application/json;charset=UTF-8");
		
		StudentWrapper studentWrapper = this.getStudentWrapper();
		String[] ids = null;
		List<Long> purchaseItemIds = new ArrayList<Long>();
		JSONObject jsonObject = null;
		try {
			ids = request.getParameterValues("purchaseItemId");
			for (String id : ids) {
				Long purchaseItemId = Long.valueOf(id);
				purchaseItemIds.add(purchaseItemId);
			}
		} catch (Exception e) {
			log.error("购置单ID错误");
			jsonObject = this.makeJSONObject(false, "购置单ID错误");
			response.getWriter().print(jsonObject.toString());
			return null;
		}
		
		try {
			studentWrapper.deletePurchaseItems(purchaseItemIds);
			jsonObject = this.makeJSONObject(true, "删除成功...");
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "删除失败" + e.getMessage() + "...");			
		}
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		return null;
	}
	
	public ActionForward purchaseItemList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentWrapper studentWrapper = this.getStudentWrapper();
		PurchaseItemQueryService purchaseItemQueryService = (PurchaseItemQueryService) ContextUtil.getContext().getBean("purchaseItemQueryService");
		PurchaseItemDTO purchaseItemDTO = (PurchaseItemDTO) form;
		Dormitory dormitory = studentWrapper.getStudent().getDormitory();
		int[] params = null;
		int start;
		int count;
		Boolean processed = purchaseItemDTO.getProcessed();
		Boolean applied = purchaseItemDTO.getApplied();
		Date fromDate = null;
		Date toDate = null;
		String type = null;
		PageIterator<PurchaseItem> purchaseItems = null;
		
		try {
			fromDate = DateFormatUtil.parseDate(purchaseItemDTO.getFromDate());
			toDate = DateFormatUtil.parseDate(purchaseItemDTO.getToDate());
		} catch (Exception e) {
			log.warn("日期解释错误...");
		}
		
		try {
			params = PageUtil.parsePageParameter(request);
			type = purchaseItemDTO.getType();
			start = params[0];
			count = params[1];
			if (type != null && type.length() != 0) {
				if (type.equalsIgnoreCase("dormitory")) {
					purchaseItems = purchaseItemQueryService.findByDormitory(start, count, dormitory, fromDate, toDate, processed, applied);
				} else if (type.equalsIgnoreCase("student")){
				
				}
			} else {
				purchaseItems = purchaseItemQueryService.findByDormitory(start, count, dormitory, fromDate, toDate, processed, applied);
			}
			request.setAttribute("purchaseItems", purchaseItems);
			request.setAttribute("student", studentWrapper.getStudent());
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
}
